package cn.com.b2office.struts2.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.com.b2office.domain.Cart;
import cn.com.b2office.domain.DeliveryMode;
import cn.com.b2office.domain.DictData;
import cn.com.b2office.domain.MemberAddress;
import cn.com.b2office.domain.PaymentMode;
import cn.com.b2office.domain.Shop;
import cn.com.b2office.domain.User;
import cn.com.b2office.service.DeliveryService;
import cn.com.b2office.service.MemberService;
import cn.com.b2office.service.OrderCustomService;
import cn.com.b2office.service.ShopService;

import com.opensymphony.xwork2.ActionSupport;

public class ShowOrderCustomizeAction extends ActionSupport implements
		SessionAware {

	private static final long serialVersionUID = 513082102510754931L;

	private Map session = null;

	private Cart cart = null;

	private MemberAddress address = null;

	private Map<String, String> addressMap = null;

	private List<PaymentMode> payModes = null;

	private List<DeliveryMode> deliveryModes = null;

	private List<DictData> invoiceKinds = null;

	private Shop shop = null;

	private MemberService memberService = null;

	private OrderCustomService orderCustomService = null;

	private ShopService shopService = null;

	private DeliveryService deliveryService = null;

	/** 各种不同配送方式在当前金额下的配送费 */
	private Map<String, Double> freights = null;

	// 当前配送地址
	private String currentAddr = null;

	public String execute() throws Exception {
		Cart cart = (Cart) session.get("cart");
		User user = (User) session.get("user");
		if (user == null)
			return "login";
		if (cart == null) {
			return "empty";
		}
		List<MemberAddress> addressList = memberService.findAllAddress(user
				.getMemberId());
		addressMap = memberService.translateAddress(addressList);
		for (MemberAddress addr : addressList) {
			if (addr.getAddidno().equals(cart.getAddressId())) {
				address = addr;
				currentAddr = addressMap.get(addr.getAddidno());
				break;
			}
		}
		// invoiceKinds = orderCustomService.findAllInvoiceKind();
		deliveryModes = deliveryService.findAllDeliveryMode();
		payModes = orderCustomService.findAllPaymentMode();

		// 计算当前金额下各种不同配送方式的配送费
		// TODO:需重构
		freights = new HashMap<String, Double>();
		for (DeliveryMode m : deliveryModes) {
			freights.put(String.valueOf(m.getIdno()), deliveryService.calculateDeliveryFee(
					m.getIdno(), cart.getSum()).getFee());
		}
		if (address != null)
			shop = shopService.getManageShop(address.getCountyid());
		return SUCCESS;

	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public void setOrderCustomService(OrderCustomService orderCustomService) {
		this.orderCustomService = orderCustomService;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public void setDeliveryService(DeliveryService deliveryService) {
		this.deliveryService = deliveryService;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public Map<String, String> getAddressMap() {
		return addressMap;
	}

	public List<PaymentMode> getPayModes() {
		return payModes;
	}

	public List<DeliveryMode> getDeliveryModes() {
		return deliveryModes;
	}

	public List<DictData> getInvoiceKinds() {
		return invoiceKinds;
	}

	public MemberAddress getAddress() {
		return address;
	}

	public String getCurrentAddr() {
		return currentAddr;
	}

	public Shop getShop() {
		return shop;
	}

	public Map<String, Double> getFreights() {
		return freights;
	}

}
